﻿using Couchbase.Core.IO.Authentication.X509;
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.HDPlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        public DeviceSerialData serial = SerialServer.serial;
        public HDProcessData _dev = SerialServer.devState;
        public HDZHDeviceSerialData hdzh_serial = SerialServer.hdzh_serial;
        //public HDZHProcessData hdzh_dev = SerialServer.hdzh_devState;
        public SHGDDeviceSerialData shgd_serial = SerialServer.shgd_serial;
        public ComPassDeviceSerialData compass_serial = SerialServer.compass_serial;
        public static RaspberryPie pie = SerialServer.raspberryPie;
        public SHGDLine18SerialData SHGDLine18SerialData = SerialServer.sHGDLine18SerialData;
        public DeviceController()
        {
        }

        /// <summary>
        /// 修改基本参数
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateDevInfo")]
        public ResponseMessage UpdateDevInfo(DevInfo devInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL da = new DeviceDAL();
                DevInfo dev = da.QueryDev();
                if (da.UpdateDevInfo(devInfo))
                {
                    /* if (dev.enable != devInfo.enable)
                     {
                         if (devInfo.enable == 1)
                         {
                             if (devInfo.protocoltype.ToUpper() == "SERVER")
                             {
                                 serial.RestartServer(devInfo.mainPort);
                             }
                             else
                             {
                                 serial.RestartClient(devInfo.mainIP, devInfo.mainPort);
                             }
                         }
                         else
                         {
                             if (dev.protocoltype.ToUpper() == "SERVER")
                             {
                                 serial.CloseServer();
                             }
                             else
                             {
                                 serial.CloseClient();
                             }
                         }
                     }
                     else
                     {
                         if (devInfo.enable == 1)
                         {
                             if (dev.protocoltype.ToUpper() == "SERVER")
                             {
                                 if (devInfo.protocoltype.ToUpper() == "SERVER")
                                 {
                                     if (devInfo.mainPort != dev.mainPort)
                                     {
                                         serial.CloseServer();
                                         serial.RestartServer(devInfo.mainPort);
                                     }
                                 }
                                 else
                                 {
                                     serial.CloseServer();
                                     serial.RestartClient(devInfo.mainIP, devInfo.mainPort);
                                 }
                             }
                             else
                             {
                                 if (devInfo.protocoltype.ToUpper() == "SERVER")
                                 {
                                     serial.CloseClient();
                                     serial.RestartServer(devInfo.mainPort);
                                 }
                                 else
                                 {
                                     if (devInfo.mainPort != dev.mainPort || dev.mainIP != devInfo.mainIP)
                                     {
                                         serial.CloseClient();
                                         serial.RestartClient(devInfo.mainIP, devInfo.mainPort);
                                     }
                                 }
                             }
                         }
                     }*/
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改设备信息失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "修改设备信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改调试开关
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateDebugSwitch")]
        public ResponseMessage UpdateDebugSwitch(DevInfo devInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL da = new DeviceDAL();
                DevInfo dev = da.QueryDev();
                if (da.UpdateSwithInfo(devInfo))
                {
                    hdzh_serial.LogPrint = devInfo.printlog;
                    serial.UpdateStatus(devInfo);
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改调试开关失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "修改调试开关异常";
            }

            return message;
        }


        /// <summary>
        /// 新增设备信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("InsertDevInfo")]
        public ResponseMessage InsertDevInfo(DevInfo devInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL da = new DeviceDAL();
                DevInfo dev = da.QueryDev();
                if (da.InsertDevInfo(devInfo))
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "新增设备信息失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "新增设备信息异常";
            }

            return message;
        }

        /// <summary>
        /// 查询设备信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryDevInfo")]
        public ResponseMessage QueryDevInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL da = new DeviceDAL();
                DevInfo dev = da.QueryDev();
                message.errcode = 0;
                message.data = dev;

            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备查询异常";
            }

            return message;
        }

        /// <summary>
        /// 查询信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryStatus")]
        public ResponseMessage QueryStatus()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ElevatorStatusInfo info = new ElevatorStatusInfo();
                info.PowerStatus = hdzh_serial.power();
                info.HDConnect = hdzh_serial.hd();
                info.TKStatus = hdzh_serial.TK();
                info.HDTKStatus = hdzh_serial.GetHdTK();
                message.data = info;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备查询异常";
            }

            return message;
        }

        /// <summary>
        /// 设备重启
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Reboot")]
        public ResponseMessage Reboot()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                pie.RunCommand("reboot");
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备查询异常";
            }

            return message;
        }

        /// <summary>
        /// 查询主副连接状态
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryConnect")]
        public ResponseMessage QueryConnect()
        {
            ResponseMessage message = new ResponseMessage();
            ConnectStatus connect = new ConnectStatus();
            DeviceDAL da = new DeviceDAL();
            try
            {
                DevInfo dev = da.QueryDev();
                if (dev.enable == 1)
                {
                    int status = 0;

                    if (dev.protocoltype.ToUpper() == "CLIENT")
                    {
                        status = hdzh_serial.GetConnectStaus(0);
                        connect.master = 0;
                    }
                    else
                    {
                        status = hdzh_serial.GetConnectStaus(1);
                        connect.master = 1;
                    }
                    connect.status = status;
                }
                else
                {
                    connect.master = 1;
                    connect.status = 0;
                }

                message.errcode = 0;
                message.data = connect;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询连接状态异常";
            }

            return message;
        }

        /// <summary>
        /// 查询mac地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMac")]
        public ResponseMessage QueryMac()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.errcode = 0;
                string macAddress = "";

                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (nic.OperationalStatus == OperationalStatus.Up)
                    {
                        macAddress = nic.GetPhysicalAddress().ToString();
                        IPInterfaceProperties ipProperties = nic.GetIPProperties();
                        UnicastIPAddressInformation curIP = ipProperties.UnicastAddresses[0];
                        string ip = curIP.Address.ToString();
                        string mask = curIP.IPv4Mask.ToString();
                        string server = ipProperties.GatewayAddresses[0].Address.ToString();
                        break;
                    }
                }
                message.data = macAddress;


                //message.data = _dev.GetMACIp();
                //message.data = hdzh_dev.GetLocalMac();
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询mac地址异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetInfo")]
        public ResponseMessage QueryNetInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.errcode = 0;
                message.data = pie.GetJMNetInfo();
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryLine18NetInfo")]
        public ResponseMessage QueryLine18NetInfo(Line18NetInfo line18NetInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                Line18NetInfo line18Net = new Line18NetInfo();
                List<Line18NetInfo> line18NetInfos = pie.GetLine18NetInfo(SHGDLine18SerialData.NetWorkNum);
                List<Line18NetInfo> line18NetInfo1 = line18NetInfos.FindAll(delegate (Line18NetInfo e) { return e.ID == line18NetInfo.ID; });
                if (line18NetInfo1 != null && line18NetInfo1.Count > 0)
                {
                    line18Net = line18NetInfo1[0];
                }
                else
                {
                    line18Net.ID = line18NetInfo.ID;
                }
                message.errcode = 0;

                message.data = line18Net;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// PING功能
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("PingTest")]
        public ResponseMessage PingTest(string ip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceBUS bus = new DeviceBUS();
                message.errcode = 0;
                message.data = bus.PingTest(ip);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "ping异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetInfo")]
        public ResponseMessage UpdateNetInfo(NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string value = string.Empty;
                string[] array = netInfo.Mask.Replace(" ", "").Split('.');
                for (int i = 0; i < array.Length; i++)
                {
                    value += Convert.ToString(Convert.ToInt32(array[i]), 2);
                }
                value = value.PadRight(32, '0');

                string keyWord = "1";
                int index = 0;
                int count = 0;
                while ((index = value.IndexOf(keyWord, index)) != -1)
                {
                    count++;
                    index = index + keyWord.Length;
                }
                netInfo.Mask = count.ToString();

                bool result = pie.UpdateJMNetInfo(netInfo);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 手动校验时间
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("CalibrationTime")]
        public ResponseMessage CalibrationTime(string time)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                bool result = pie.CalibrationTime(time);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "校验时间失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 读取ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetNtpIp")]
        public ResponseMessage GetNtpIp()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string result = pie.GetNtpIp();

                message.errcode = 0;
                message.data = result;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 读取ntp服务器地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNtpIp")]
        public ResponseMessage UpdateNtpIp(string ip)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                bool result = pie.SetNtp(ip);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改Ntp服务器失败";
                }


            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Ntp服务器地址异常";
            }

            return message;
        }

        /// <summary>
        /// 查询应用类型
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAuthModeList")]
        public ResponseMessage QueryAuthModeList()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                List<AuthMode> list = deviceDAL.QueryAuthMode();
                message.data = list;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询应用类型失败";
            }

            return message;
        }

        /// <summary>
        ///更新应用类型
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateAuthMode")]
        public ResponseMessage UpdateAuthMode(DevInfo dev)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                bool result = deviceDAL.UpdateAuthMode(dev);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新失败";
                }

            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///查询Mqtt信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMqttInfo")]
        public ResponseMessage QueryMqttInfo()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                MqttInfo list = deviceDAL.QueryMqttInfo();
                list.topic = AppSettingsHelper.Configuration["Topic"];
                message.data = list;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Mqtt信息失败";
            }

            return message;

        }

        /// <summary>
        ///更新Mqtt信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateMqttInfo")]
        public ResponseMessage UpdateMqttInfo(MqttInfo mqttInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                bool result = deviceDAL.UpdateMqttInfo(mqttInfo);
                if (result)
                {
                    string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                    var filePath = contentPath + "appsettings.json";
                    JObject jsonObject;
                    using (StreamReader file = new StreamReader(filePath))
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        jsonObject = (JObject)JToken.ReadFrom(reader);
                        jsonObject["Topic"] = mqttInfo.topic;
                    }

                    using (var writer = new StreamWriter(filePath))
                    using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                    {
                        jsonObject.WriteTo(jsonwriter);
                    }
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新失败";
                }

            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///更新Mqtt信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateConfig")]
        public ResponseMessage UpdateConfig(int MqttSend)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["MqttSend"] = MqttSend;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
                shgd_serial.mqttsend = MqttSend;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///导入证书
        /// </summary>
        /// <param name="fileName">证书路径</param> 
        ///  <param name="pwd">证书密码</param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("ImportPfx")]
        public ResponseMessage ImportPfx(string fileName, string pwd)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                X509Certificate2 pc = new X509Certificate2(fileName, pwd);
                string publikey = pc.PublicKey.Key.ToXmlString(false);
                string privatekey = pc.PrivateKey.ToXmlString(true);
                using (FileStream fileStream = new FileStream("wwwroot/PublicKey.xml", FileMode.Create))
                {
                    byte[] buffer = Encoding.Default.GetBytes(publikey);
                    for (int i = 0; i < buffer.Length; i++)
                        fileStream.WriteByte(buffer[i]);
                    fileStream.Seek(0, SeekOrigin.Begin);
                    fileStream.Close();
                    SerialServer.publickey = publikey;
                }
                using (FileStream fileStream = new FileStream("wwwroot/Private.xml", FileMode.Create))
                {
                    byte[] buffer = Encoding.Default.GetBytes(privatekey);
                    for (int i = 0; i < buffer.Length; i++)
                        fileStream.WriteByte(buffer[i]);
                    fileStream.Seek(0, SeekOrigin.Begin);
                    fileStream.Close();
                    SerialServer.publickey = publikey;
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "导入异常";
                LogHelper.Log(ex.ToString());
            }
            return message;
        }

        /// <summary>
        ///是否使用加密
        /// </summary>
        /// <param name="Encry">是否加密，0=不加密，1=加密</param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateEncry")]
        public ResponseMessage UpdateEncry(int Encry)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                if (Encry == 1)
                {
                    string filepath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/PublicKey.xml";
                    if (!System.IO.File.Exists(filepath))
                    {
                        message.errcode = 1;
                        message.errmsg = "请先导入证书";
                        return message;
                    }
                }
                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["Encry"] = Encry;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
                SerialServer.Encry = Encry;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        ///查询是否使用加密
        /// </summary>
        /// <param name="Encry">是否加密，0=不加密，1=加密</param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryEncry")]
        public ResponseMessage QueryEncry()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.data = Convert.ToInt32(AppSettingsHelper.Configuration["Encry"]);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "读取异常";
            }

            return message;
        }

        ///是否使用数据模拟
        /// </summary>
        /// <param name="Encry">是否加密，0=不加密，1=加密</param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("DataSimulation")]
        public ResponseMessage DataSimulation(DataSimulationInfo dataSimulation)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["DataSimulation"] = dataSimulation.DataSimulation;
                    jsonObject["VirtualInterval"] = dataSimulation.VirtualInterval;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设置失败";
            }

            return message;
        }

        ///查询是否使用数据模拟
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryDataSimulation")]
        public ResponseMessage QueryDataSimulation()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DataSimulationInfo dataSimulation = new DataSimulationInfo();
                dataSimulation.DataSimulation = Convert.ToInt32(AppSettingsHelper.Configuration["DataSimulation"]);
                dataSimulation.VirtualInterval = Convert.ToInt32(AppSettingsHelper.Configuration["VirtualInterval"]);
                message.data = dataSimulation;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }

            return message;
        }

        ///更新数据模拟
        /// </summary>
        /// <param name="list"></param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateSimulationData")]
        public ResponseMessage UpdateSimulationData(List<MqttDeviceInfoDataPoint> list)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                shgd_serial.dataSimulation_point.Clear();
                shgd_serial.dataSimulation_point = list;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新失败";
            }

            return message;
        }

        ///查询实时数据
        /// </summary>
        /// <param name="list"></param> 
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryData")]
        public ResponseMessage QueryData()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                List<MqttDeviceInfoDataPoint> list = new List<MqttDeviceInfoDataPoint>();
                List<MqttDeviceInfoDataPoint> list_state = new List<MqttDeviceInfoDataPoint>();
                int mode = shgd_serial.DataSimulation;
                if (mode == 0)
                {
                    list_state = shgd_serial.last_point;
                    if (list_state == null || list_state.Count == 0)
                    {
                        MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_run";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_pks";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_ins";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_fault";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_healtbeat";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_fault";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dirction";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);
                    }
                    else
                    {
                        if (shgd_serial.ele_type == 1)
                        {
                            MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_run";
                            List<MqttDeviceInfoDataPoint> dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "yxzt"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_pks";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "stzt"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_ins";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "jxzt"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_fault";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "gz"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_healtbeat";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "txxt"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_fault";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_dirction";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);
                        }
                        else
                        {
                            MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_dirction";
                            List<MqttDeviceInfoDataPoint> dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "yxfx"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_fault";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "gz"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "jtbj"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "gbfdbj"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                            dataPoints = list_state.FindAll(delegate (MqttDeviceInfoDataPoint e) { return e.point == "txxt"; });
                            mqttDeviceInfoDataPoint.value = dataPoints[0].value;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_run";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_pks";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_ins";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_fault";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);

                            mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                            mqttDeviceInfoDataPoint.point = "dt_healtbeat";
                            mqttDeviceInfoDataPoint.value = 0;
                            list.Add(mqttDeviceInfoDataPoint);
                        }
                    }
                }
                else if (mode == 3)
                {
                    DTParamInfo dTParamInfo = shgd_serial.dTParamInfo;
                    FTParamInfo fTParamInfo = shgd_serial.fTParamInfo;
                    MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "dt_run";
                    if (dTParamInfo.CZDT_YX_ZT != null && dTParamInfo.CZDT_YX_ZT.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);


                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "dt_pks";
                    if (dTParamInfo.CZDT_ST_ZT != null && dTParamInfo.CZDT_ST_ZT.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "dt_ins";
                    if (dTParamInfo.CZDT_JX_ZT != null && dTParamInfo.CZDT_JX_ZT.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "dt_fault";
                    if (dTParamInfo.CZDT_GZ_BJ != null && dTParamInfo.CZDT_GZ_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "dt_healtbeat";
                    if (dTParamInfo.CZDT_TX_BJ != null && dTParamInfo.CZDT_TX_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "ft_fault";
                    if (fTParamInfo.ZDFT_GZ_BJ != null && fTParamInfo.ZDFT_GZ_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                    if (fTParamInfo.ZDFT_TX_BJ != null && fTParamInfo.ZDFT_TX_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "ft_dirction";
                    if (fTParamInfo.ZDFT_SX_ZT != null && fTParamInfo.ZDFT_SX_ZT.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else if (fTParamInfo.ZDFT_XX_ZT != null && fTParamInfo.ZDFT_XX_ZT.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 2;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                    if (fTParamInfo.ZDFT_GB_BJ != null && fTParamInfo.ZDFT_GB_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);

                    mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                    mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                    if (fTParamInfo.ZDFT_JT_BJ != null && fTParamInfo.ZDFT_JT_BJ.value == true)
                    {
                        mqttDeviceInfoDataPoint.value = 1;
                    }
                    else
                    {
                        mqttDeviceInfoDataPoint.value = 0;
                    }
                    list.Add(mqttDeviceInfoDataPoint);
                }
                else
                {
                    list = shgd_serial.dataSimulation_point;
                    if (list.Count == 0)
                    {
                        MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_run";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_pks";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_ins";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_fault";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "dt_healtbeat";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_fault";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dirction";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);
                    }
                    else
                    {
                        MqttDeviceInfoDataPoint mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_fault";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_healtbeat";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dirction";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_dover_Plate_Alarm";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);

                        mqttDeviceInfoDataPoint = new MqttDeviceInfoDataPoint();
                        mqttDeviceInfoDataPoint.point = "ft_emergency_Stop";
                        mqttDeviceInfoDataPoint.value = 0;
                        list.Add(mqttDeviceInfoDataPoint);
                    }
                }
                message.data = list;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "查询异常";
            }

            return message;
        }

        /// <summary>
        ///更新外呼命令延时时间、是否启用梯控
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateIntervalHeartCheck")]
        public ResponseMessage UpdateIntervalHeartCheck(CemsConfig cemsConfig)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["OutboundInterval"] = cemsConfig.Interval;
                    jsonObject["HeartCheck"] = cemsConfig.HeartCheck;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///查询外呼命令延时时间、是否启用梯控
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryCemsConfig")]
        public ResponseMessage QueryCemsConfig()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CemsConfig cemsConfig = new CemsConfig();
                cemsConfig.Interval = Convert.ToInt32(AppSettingsHelper.Configuration["OutboundInterval"]);
                cemsConfig.HeartCheck = Convert.ToInt32(AppSettingsHelper.Configuration["HeartCheck"]);

                message.data = cemsConfig;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询串口信息失败";
            }

            return message;
        }

        /// <summary>
        ///查询串口信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryCemsSerialInfo")]
        public ResponseMessage QueryCemsSerialInfo(CemsSerialInfo cemsSerialInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                CemsSerialInfo serialInfo = new CemsSerialInfo();
                serialInfo.SerialId = cemsSerialInfo.SerialId;
                if (cemsSerialInfo.SerialId == 1)
                {
                    serialInfo.BaudRate = Convert.ToInt32(AppSettingsHelper.Configuration["BaudRate"]);
                    serialInfo.Parity = Convert.ToInt32(AppSettingsHelper.Configuration["Parity"]);
                    serialInfo.DataBits = Convert.ToInt32(AppSettingsHelper.Configuration["DataBits"]);
                    serialInfo.StopBits = Convert.ToInt32(AppSettingsHelper.Configuration["StopBits"]);
                    serialInfo.PortName = AppSettingsHelper.Configuration["PortName"];
                }
                else if (cemsSerialInfo.SerialId == 2)
                {
                    serialInfo.BaudRate = Convert.ToInt32(AppSettingsHelper.Configuration["BaudRate_2"]);
                    serialInfo.Parity = Convert.ToInt32(AppSettingsHelper.Configuration["Parity_2"]);
                    serialInfo.DataBits = Convert.ToInt32(AppSettingsHelper.Configuration["DataBits_2"]);
                    serialInfo.StopBits = Convert.ToInt32(AppSettingsHelper.Configuration["StopBits_2"]);
                    serialInfo.PortName = AppSettingsHelper.Configuration["PortName_2"];
                }

                message.data = serialInfo;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询串口信息失败";
            }

            return message;
        }

        /// <summary>
        ///更新串口配置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateSerialConfig")]
        public ResponseMessage UpdateSerialConfig(CemsSerialInfo cemsSerialInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    if (cemsSerialInfo.SerialId == 1)
                    {
                        jsonObject["BaudRate"] = cemsSerialInfo.BaudRate;
                        jsonObject["Parity"] = cemsSerialInfo.Parity;
                        jsonObject["DataBits"] = cemsSerialInfo.DataBits;
                        jsonObject["StopBits"] = cemsSerialInfo.StopBits;
                        jsonObject["PortName"] = cemsSerialInfo.PortName;
                    }
                    else if (cemsSerialInfo.SerialId == 2)
                    {
                        jsonObject["BaudRate_2"] = cemsSerialInfo.BaudRate;
                        jsonObject["Parity_2"] = cemsSerialInfo.Parity;
                        jsonObject["DataBits_2"] = cemsSerialInfo.DataBits;
                        jsonObject["StopBits_2"] = cemsSerialInfo.StopBits;
                        jsonObject["PortName_2"] = cemsSerialInfo.PortName;
                    }
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///查询人脸面板端口号、CEMS平台地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryFacePort")]
        public ResponseMessage QueryFacePort()
        {
            ResponseMessage message = new ResponseMessage();
            PlatformConfig platformConfig = new PlatformConfig();
            try
            {

                platformConfig.FacePort = Convert.ToInt32(AppSettingsHelper.Configuration["ComPassPort"]);
                platformConfig.CemsPlatformIp = AppSettingsHelper.Configuration["ComPassPlatformIp"];
                message.data = platformConfig;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询串口信息失败";
            }

            return message;
        }

        /// <summary>
        ///更新人脸面板端口号、cems平台地址
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateFacePort")]
        public ResponseMessage UpdateFacePort(PlatformConfig platformConfig)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["ComPassPort"] = platformConfig.FacePort;
                    jsonObject["ComPassPlatformIp"] = platformConfig.CemsPlatformIp;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        ///查询18号线Mqtt信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMqttLine18Info")]
        public ResponseMessage QueryMqttLine18Info()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                MqttInfo list = deviceDAL.QueryMqttInfo();
                list.topic = AppSettingsHelper.Configuration["Topic"];
                list.SerialNumber = AppSettingsHelper.Configuration["SerialNumber"];
                list.DeviceCode = AppSettingsHelper.Configuration["DeviceCode"];
                message.data = list;
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询Mqtt信息失败";
            }

            return message;

        }

        /// <summary>
        ///更新Mqtt信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateMqttLine18Info")]
        public ResponseMessage UpdateMqttLine18Info(MqttInfo mqttInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceDAL deviceDAL = new DeviceDAL();
                bool result = deviceDAL.UpdateMqttInfo(mqttInfo);
                if (result)
                {
                    string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                    var filePath = contentPath + "appsettings.json";
                    JObject jsonObject;
                    using (StreamReader file = new StreamReader(filePath))
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {
                        jsonObject = (JObject)JToken.ReadFrom(reader);
                        jsonObject["Topic"] = mqttInfo.topic;
                        jsonObject["SerialNumber"] = mqttInfo.SerialNumber;
                        jsonObject["DeviceCode"] = mqttInfo.DeviceCode;
                    }

                    using (var writer = new StreamWriter(filePath))
                    using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                    {
                        jsonObject.WriteTo(jsonwriter);
                    }
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "更新失败";
                }

            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateLine18NetInfo")]
        public ResponseMessage UpdateLine18NetInfo(Line18NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                bool result = pie.UpdateLine18NetInfo(netInfo, SHGDLine18SerialData.NetWorkNum);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetNum")]
        public ResponseMessage QueryNetNum()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.errcode = 0;
                message.data = Convert.ToInt32(AppSettingsHelper.Configuration["NetWorkNum"]);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetNum")]
        public ResponseMessage UpdateNetNum(int num)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["NetWorkNum"] = num;
                    SHGDLine18SerialData.NetWorkNum = num;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }
    }
}
